package com.whfp.coky.ui.main

import android.view.View
import androidx.fragment.app.Fragment
import com.gyf.immersionbar.ImmersionBar
import com.whfp.coky.app.AppDatas
import com.whfp.coky.databinding.ActivityTaskMainBinding
import com.whfp.coky.manager.TabManage
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.main.frag.TaskFixedFrag
import com.whfp.coky.ui.main.frag.TaskNonFixedFrag
import com.whfp.coky.ui.main.vm.TaskMainVM
import com.whfp.coky.widget.AniUitls.Companion.fadeIn
import com.whfp.coky.widget.XCollapsingToolbarLayout
import com.whfp.coky.widget.XCollapsingToolbarLayout.OnScrimsListener

/**
 *
 * 登录后的用户主界面
 * created by coky on 2022/3/2
 */
class TaskMainAct: BaseActivity<TaskMainVM, ActivityTaskMainBinding>(),OnScrimsListener {

    private val tabTexts = arrayOf(Constants.TAB_FIXED_TASK, Constants.TAB_NON_FIXED_TASK)
    private val fragments :Array<Fragment> = arrayOf(
        TaskFixedFrag.newInstance(),
        TaskNonFixedFrag.newInstance()
    )

    override fun initView() {
        vBinding.ctlHomeBar.setOnScrimsListener(this)
        ImmersionBar.setTitleBar(getActivity(), vBinding.tbHomeTitle)
        TabManage(vBinding.tabLayout,vBinding.viewpager2).initTab(this,tabTexts,fragments)

//        vBinding.viewpager2.adapter = object : FragmentStateAdapter(
//           this
//        ) {
//            override fun getItemCount(): Int {
//                return fragments.size
//            }
//
//            override fun createFragment(position: Int): Fragment {
//                return fragments[position]
//            }
//        }
//        TabLayoutMediator(vBinding.tabLayout, vBinding.viewpager2) { tab, position ->
//            tab.text = tabTexts[position]
//        }.attach()
    }

    override fun initData() {
        AppDatas.getInstance().staffBean?.let {
            vBinding.tvNameSex.text ="${it.trueName}      ${it.sex}"
            vBinding.tvRole.text = it.roleName
            vBinding.tvStreet.text = it.orgName
            vBinding.tvName.text = it.trueName
        }
        setOnClickListener(vBinding.tvLogout,vBinding.tvLogout2)
    }

    override fun onClick(view: View) {
        when(view){
            vBinding.tvLogout -> loginOut()
            vBinding.tvLogout2 -> loginOut()
        }
    }

    private fun loginOut() {
        toast("进行退出登录...")
    }


    override fun isStatusBarDarkFont(): Boolean {
        return vBinding.ctlHomeBar.isScrimsShown()
    }

//    缩放隐藏
    override fun onScrimsStateChange(layout: XCollapsingToolbarLayout?, shown: Boolean) {
        //        mToolbar.setVisibility(shown? View.VISIBLE:View.GONE);
        getStatusBarConfig().statusBarDarkFont(shown).init()
        if (shown) {
            mFadeIn(vBinding.tbHomeTitle)
        } else {
            vBinding.tbHomeTitle.setVisibility(View.GONE)
        }
    }

    fun mFadeIn(view:View){
        fadeIn(view, 0f, 1f, 400)
        // We disabled the button in fadeOut(), so enable it here.
        view.setEnabled(true)
    }

}